package org.zch.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zch.common.ApiResult;
import org.zch.entity.po.PublicBeanPO;
import org.zch.entity.po.PublicActivityPO;
import org.zch.entity.po.PublicBannerPO;
import org.zch.entity.po.PublicPO;
import org.zch.service.PublicActivityService;
import org.zch.service.PublicBannerService;
import org.zch.service.PublicService;
import org.zch.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * 广场
 */
@RestController
@RequestMapping("/public")
@Api(tags = "广场")
public class PublicController {

    @Autowired
    private UserService userService;

    @Autowired
    private PublicService publicService;

    @Autowired
    private PublicBannerService publicBannerService;

    @Autowired
    private PublicActivityService publicActivityService;

    /**
     *获取广场数据
     */
    @ApiOperation(value = "获取广场数据", notes = "获取广场数据")
    @PostMapping("/getPublic")
    public ApiResult getPublic(@RequestParam("code") String code){
        PublicBeanPO publicBeanPO = new PublicBeanPO();
        publicBeanPO.setSysTem(publicService.findALl());
        publicBeanPO.setBannerList(publicBannerService.findAll());
        //广场底部活动
        //1.用户没有绑定手机号
        //2.用户没有添加收货地址
        List<PublicActivityPO> mBottom = new ArrayList<>();
        String uPhone = userService.getUserPhoneById(code);
        if (uPhone == null || uPhone.isEmpty()){
            PublicActivityPO dPhone = new PublicActivityPO();
            dPhone.setTitle("当前还未绑定手机号，绑定手机号可在其他设备同步数据~");
            dPhone.setBigPic("http://hualao.icu/images/bd.png");
            dPhone.setType("PHONE_ACTIVITY");
            mBottom.add(dPhone);
        }
        String uAdd = userService.getUserAddressById(code);
        if (uAdd == null || uAdd.isEmpty()){
            PublicActivityPO dAdd = new PublicActivityPO();
            dAdd.setTitle("前往添加收货地址吧，未填写收货地址不可兑换商品~");
            dAdd.setBigPic("http://hualao.icu/images/sh.png");
            dAdd.setType("ADD_GOOD_ACTIVITY");
            mBottom.add(dAdd);
        }
        mBottom.addAll(publicActivityService.findAll());
        publicBeanPO.setActivityList(mBottom);
        return ApiResult.ok(publicBeanPO);
    }

    //获取系统消息
    @ApiOperation(value = "获取系统消息", notes = "获取系统消息")
    @GetMapping("/getDataSysTem")
    public ApiResult getDataSysTem(){
        return ApiResult.ok(publicService.findALl());
    }

    //修改消息
    @ApiOperation(value = "修改消息", notes = "修改消息")
    @PostMapping("/upDataSysTem")
    public ApiResult upDataSysTem(@RequestBody PublicPO publicPO){
        publicPO.setId(1);
        return ApiResult.ok(publicService.upDataSysTem(publicPO));
    }


    //获取顶部轮播图
    @ApiOperation(value = "获取顶部轮播图", notes = "获取顶部轮播图")
    @GetMapping("/getBanner")
    public ApiResult getBanner(){
        return ApiResult.ok(publicBannerService.findAll());
    }

    //添加顶部轮播图
    @ApiOperation(value = "添加顶部轮播图", notes = "添加顶部轮播图")
    @PostMapping("/addBanner")
    public ApiResult addBanner(@RequestBody PublicBannerPO publicBannerPO){
        return ApiResult.ok(publicBannerService.addBanner(publicBannerPO));
    }

    //删除顶部轮播图
    @ApiOperation(value = "删除顶部轮播图", notes = "删除顶部轮播图")
    @PostMapping("/delBanner")
    public ApiResult delBanner(@RequestParam("id") String id){
        return ApiResult.ok(publicBannerService.delBanner(Integer.parseInt(id)));
    }

    //获取底部活动
    @ApiOperation(value = "获取底部活动", notes = "获取底部活动")
    @GetMapping("/getActivity")
    public ApiResult getActivity(){
        return ApiResult.ok(publicActivityService.findAll());
    }


    //添加底部活动
    @ApiOperation(value = "添加底部活动", notes = "添加底部活动")
    @PostMapping("/addActivity")
    public ApiResult addActivity(@RequestBody PublicActivityPO publicActivityPO){
        return ApiResult.ok(publicActivityService.addActivity(publicActivityPO));
    }


    //删除底部活动
    @ApiOperation(value = "删除底部活动", notes = "删除底部活动")
    @PostMapping("/delActivity")
    public ApiResult delActivity(@RequestParam("id") String id){
        return ApiResult.ok(publicActivityService.delActivity(Integer.parseInt(id)));
    }

}
